/*
 * @(#)DataBaseConfig.java   1.0  2021年11月9日
 * 
 * Copyright (c)	2014-2020. All Rights Reserved.	GuangZhou hhmk Technology Company LTD.
 */
package com.swift.dao.db.datasource;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Properties;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;

import com.swift.core.env.AbstractConfigInterface;
import com.swift.exception.extend.SystemException;
import com.swift.util.type.TypeUtil;

/**
 * 添加说明 
 * @author zhengjiajin
 * @version 1.0 2021年11月9日
 */
@Component
public class FileDataBaseConfig extends AbstractConfigInterface<DataBase>{

    @Value("${jdbc.driverClassName:com.mysql.cj.jdbc.Driver}")
    private String driverClassName;
    
    @Value("${jdbc.minPoolSize:5}")
    private Integer minPoolSize;
    
    @Value("${jdbc.maxPoolSize:100}")
    private Integer maxPoolSize;
    
    @Value("${classpath*:jdbc.properties}")
    private Resource resource;
    
    public List<DataBase> loadNew(){
        if (resource == null || !resource.exists()) return null;
        Properties properties = new Properties();
        try {
            properties.load(resource.getInputStream());
        } catch (IOException e) {
            throw new SystemException("加载jdbc.properties异常",e);
        }
        List<DataBase> list = new ArrayList<>();
        
        for (Object keyObj : properties.keySet()) {
            String key = TypeUtil.toString(keyObj);
            if(TypeUtil.isNull(key) || key.indexOf("jdbc.")==-1 || key.indexOf(".url")==-1) {
                continue;
            }
            String source = key.substring(key.indexOf("jdbc.")+5, key.indexOf(".url"));
            
            String driver = properties.getProperty("jdbc." + source + ".driverClassName");
            String max = properties.getProperty("jdbc." + source + ".maxPoolSize");
            String min = properties.getProperty("jdbc." + source + ".minPoolSize");
            String urls = properties.getProperty("jdbc." + source + ".url");
            String userNames = properties.getProperty("jdbc." + source + ".username");
            String passwords = properties.getProperty("jdbc." + source + ".password");
            if (TypeUtil.isNull(urls) || TypeUtil.isNull(userNames) || TypeUtil.isNull(passwords)) throw new SystemException("加载"+source+"未配置对应的信息");
            DataBase dataBase = new DataBase();
            dataBase.setKey(source);
            dataBase.setDriverClassName(TypeUtil.isNull(driver)?driverClassName:driver);
            dataBase.setMaxPoolSize(TypeUtil.toInt(max,maxPoolSize));
            dataBase.setMinPoolSize(TypeUtil.toInt(min,minPoolSize));
            String[] urlSpilt = urls.split(",");
            String[] userNameSpilt = userNames.split(",");
            String[] passwordSpilt = passwords.split(",");
            dataBase.setUrl(urlSpilt);
            dataBase.setPassword(passwordSpilt);
            dataBase.setUserName(userNameSpilt);
            list.add(dataBase);
        }
        if(TypeUtil.isNull(list)) return null;
        return Collections.unmodifiableList(list);
    }

}
